package com.ruoyi.teach.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teach.mapper.NbQuestionMapper;
import com.ruoyi.teach.domain.NbQuestion;
import com.ruoyi.teach.service.INbQuestionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 题目Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-08
 */
@Service
public class NbQuestionServiceImpl implements INbQuestionService 
{
    @Autowired
    private NbQuestionMapper nbQuestionMapper;

    /**
     * 查询题目
     * 
     * @param id 题目主键
     * @return 题目
     */
    @Override
    public NbQuestion selectNbQuestionById(Long id)
    {
        return nbQuestionMapper.selectNbQuestionById(id);
    }

    /**
     * 查询题目列表
     * 
     * @param nbQuestion 题目
     * @return 题目
     */
    @Override
    public List<NbQuestion> selectNbQuestionList(NbQuestion nbQuestion)
    {
        return nbQuestionMapper.selectNbQuestionList(nbQuestion);
    }

    /**
     * 新增题目
     * 
     * @param nbQuestion 题目
     * @return 结果
     */
    @Override
    public int insertNbQuestion(NbQuestion nbQuestion)
    {
        nbQuestion.setCreateTime(DateUtils.getNowDate());
        return nbQuestionMapper.insertNbQuestion(nbQuestion);
    }

    /**
     * 修改题目
     * 
     * @param nbQuestion 题目
     * @return 结果
     */
    @Override
    public int updateNbQuestion(NbQuestion nbQuestion)
    {
        nbQuestion.setUpdateTime(DateUtils.getNowDate());
        return nbQuestionMapper.updateNbQuestion(nbQuestion);
    }

    /**
     * 批量删除题目
     * 
     * @param ids 需要删除的题目主键
     * @return 结果
     */
    @Override
    public int deleteNbQuestionByIds(Long[] ids)
    {
        return nbQuestionMapper.deleteNbQuestionByIds(ids);
    }

    /**
     * 删除题目信息
     * 
     * @param id 题目主键
     * @return 结果
     */
    @Override
    public int deleteNbQuestionById(Long id)
    {
        return nbQuestionMapper.deleteNbQuestionById(id);
    }

    /**
     * 批量添加题目
     * @param questions
     * @return
     */
    @Override
    @Transactional
    public int batchQuestions(List<JSONObject> questions, Long id){

        List<NbQuestion> list = new ArrayList<>();
        for(JSONObject nb : questions){
            // 转换为questions、对象
            NbQuestion nbQuestion = JSONObject.parseObject(nb.toJSONString(), NbQuestion.class);
            nbQuestion.setTeacherId(id);
            nbQuestion.setCreateTime(DateUtils.getNowDate());
            list.add(nbQuestion);
            System.out.println("question:"+nbQuestion);
        }
        return nbQuestionMapper.batchQuestions(list);
    }
}
